﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using LibraryO2S;
using System.IO;
using BusinessObjects;

public partial class controls_admin_AdvertisingAdd : Pages_Base
{
    private DataTable objDT;
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            loadSetting();
            base.loadModuleByType(ddlModule, 6);
            if (Request["m_id"] != null)
            {
                string m_id = Request["m_id"].ToString();
                ddlModule.SelectedValue = m_id;
            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        base.OpenConnection();
        string fileName = String.Empty;
        try
        {
            if (fulAdv.HasFile)
            {
                fileName = fulAdv.FileName;
                int count = 0;
                while (File.Exists(Server.MapPath("~/upload/ads/") + fileName))
                {
                    fileName = count + "_" + fulAdv.FileName;
                    count++;
                }
                fulAdv.SaveAs(Server.MapPath("~/upload/ads/") + fileName);
            }
        }
        catch (Exception)
        {
            divMessage.InnerHtml = O2S_Message.Error("Hệ Thống", "Lỗi Ghi file!");
            return;
        }
        try
        {
            string title = txtTitle.Text.Trim();
            string url = txtUrl.Text.Trim();
            int module_id = Convert.ToInt32(ddlModule.SelectedValue);
            int sortDisplay = Convert.ToInt32(txtSortDisplay.Text);
            bool display = chbDisplay.Checked;
            DateTime endDate;
            DateTime.TryParse(txtExpired.Text.Trim(), out endDate);
            DataTable admin = (DataTable)Session["Admin"];
            string result = BO_Ads_Picture.Insert(module_id, title
                    , fileName, url, display, sortDisplay, DateTime.Now, admin.Rows[0]["USER_NAME"].ToString(), DateTime.Now, endDate, "", true);
            if (result.Equals("0"))
                divMessage.InnerHtml = O2S_Message.Error("Thông Báo", "Lỗi trong quá trình truyền tải dữ liệu. Mong bạn trở lại sau .");
            else
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Thực hiện thành công .");
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi xảy ra !<br />" + ex.Message);
        }
        finally { base.CloseConnection(); }
    }
    private void loadSetting()
    {
        objDT = BO_Settings.SelectByGroupId(7, 1);
        foreach (DataRow dr in objDT.Rows)
        {
            string value = dr["SETTING_VALUE"].ToString();
            switch (dr["SETTING_KEY"].ToString())
            {
                case "NOTE_ADS_DISPLAY":
                    NOTE_ADS_DISPLAY.InnerText = value;
                    break;
                case "NOTE_ADS_ENDDATE":
                    NOTE_ADS_ENDDATE.InnerText = value;
                    break;
                case "NOTE_ADS_LINK":
                    NOTE_ADS_LINK.InnerText = value;
                    break;
                case "NOTE_ADS_MODULE":
                    NOTE_ADS_MODULE.InnerText = value;
                    break;
                case "NOTE_ADS_SORT":
                    NOTE_ADS_SORT.InnerText = value;
                    break;
                case "NOTE_ADS_TITLE":
                    NOTE_ADS_TITLE.InnerText = value;
                    break;
                case "NOTE_ADS_URL":
                    NOTE_ADS_URL.InnerText = value;
                    break;
            }
        }
    }
}
